package com.twu.biblioteca;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

public class StartConsoleApp {
    static void consoleAppStart() {
        System.out.print("Do you want to login?(Enter Y or N)");
        BufferedReader bufferRead = new BufferedReader(new InputStreamReader(System.in));
        try {
            String s = bufferRead.readLine();
            if (s.equalsIgnoreCase("Y")) {
                callLoginMenu();
            } else
                showMenuOption("");

        } catch (IOException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }
    }
    private static void callLoginMenu() throws IOException {

        String userLibraryNumber="";
        String pswd="";
        BufferedReader bufferRead = new BufferedReader(new InputStreamReader(System.in));
        System.out.print("Enter UserName");
        userLibraryNumber=  bufferRead.readLine();
        System.out.print("Enter password");
        pswd=  bufferRead.readLine();
        ValidateUser v = new ValidateUser();
        //verify userExistance and pasword for the same user
        if(v.checkUserExistance(userLibraryNumber).equals("true") && pswd.equals(v.getPassword(userLibraryNumber)))
        {
            System.out.print("Welcome"+v.getName(userLibraryNumber)+"!!!");
            showMenuOption(userLibraryNumber);

        }
        else{
            System.out.println("Invalid username or password.");
            callLoginMenu();
        }

    }

    private static void showMenuOption(String userLibraryNumber) throws IOException {

        String selectedOption="";
        while(true)      {

            selectedOption= showMenu(userLibraryNumber);
            if(selectedOption.equalsIgnoreCase("D")){
                callShowDetails(userLibraryNumber);
            }
            else if(selectedOption.equalsIgnoreCase("L")){
                callListBooks();
            }
            else if(selectedOption.equalsIgnoreCase("M")){
                callMovieInfo();
            }
            else if(selectedOption.equals("R")){
                callReserveBook(userLibraryNumber);
            }
            else if(selectedOption.equalsIgnoreCase("Q")){
                break;
            }
            else
                System.out.println("Enter valid option");
        }
    }

    private static void callReserveBook(String userLibraryNumber) {
        //List the boooks
        callListBooks();
        System.out.println("Do you want to reserve any book?");
        BufferedReader bufferRead = new BufferedReader(new InputStreamReader(System.in));

        try {
            if(bufferRead.readLine().equalsIgnoreCase("y")){
                System.out.println("Please specify the name of the book.");
                String selectedBook =  bufferRead.readLine();
                String messageString = "";
                //Check for availibility of book
                ReserveBook rb = new ReserveBook();
                Boolean isexists = rb.reserveSpecifiedBook(selectedBook);
                if (isexists == true)
                    messageString = "Thank You! Enjoy the book.";

                else
                    messageString = "Sorry we don't have that book yet";
                System.out.println(messageString);

            }
            else
                showMenuOption(userLibraryNumber);
        }catch (IOException e){
            e.printStackTrace();
        }


    }

    private static void callMovieInfo() {
        MovieInformation m = new MovieInformation();
        //Add movie from sample database MovieInformation
        m.createDatabase();
        HashMap<String, ArrayList> a = m.getMovieInfo();
        System.out.println("Movie Name"+"|"+"Year"+"|"+"Director"+"|"+"Rating\n");
        for (Map.Entry<String, ArrayList> e : a.entrySet()) {
            //check for rating if is it null or not
            if(null==e.getValue().get(2))
                System.out.println(e.getKey() + "|" + e.getValue().get(0) + "|" + e.getValue().get(1) + "|" + "N/A");
            else
                System.out.println(e.getKey() + "|" + e.getValue().get(0) + "|" + e.getValue().get(1) + "|" + e.getValue().get(2));
        }
    }

    private static void callListBooks() {
        //Get list of books from sample database  and add to list model
        BooksSampleDatabase s = new BooksSampleDatabase();
        s.createDatabase();
        for (String BookName : s.getBookList())
            System.out.println(BookName);
    }

    private static void callShowDetails(String userLibraryNumber) {
        //check authetication by using userLibraryNumber if it is null means user has not logged in
        if(userLibraryNumber.equals(""))
            System.out.println("Please contact to Librarian.Thank You");
        else
        {
            //Get userDetails using userLibraryNumber
            ValidateUser vu = new ValidateUser();
            try {
                System.out.println("UserName:" + vu.getName(userLibraryNumber));
                System.out.println("EmailId:" + vu.getEmailID(userLibraryNumber));
                System.out.println("Phone Number:" + vu.getPhoneNumber(userLibraryNumber)+"\n");
            } catch (NullPointerException e) {
                e.printStackTrace();
            }
        }
    }

    private static String showMenu(String userLibraryNumber) {
        BufferedReader bufferRead = new BufferedReader(new InputStreamReader(System.in));
        String selectedOption="";
        System.out.print("\n");
        System.out.println("Select the menu option");
        System.out.println("D :Check Details");
        System.out.println("L :List Books");
        System.out.println("M :Movie Information");
        if (!userLibraryNumber.equals(""))
            System.out.println("R :Reserve Book");
        System.out.println("Q :Exit");
        try {
            selectedOption=bufferRead.readLine();
        } catch (IOException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }
        return selectedOption;
    }

}